﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;

namespace _4T
{
    public partial class CMS_AddStation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["s"] != null)
            {
                if (!IsPostBack)
                {
                    string stationID = Request.QueryString["s"].ToString();
                    LoadStation(stationID);
                    hdfStationID.Value = stationID;
                }
            }
        }

        private void LoadStation(string stationID)
        {
            CMS_Station station = new StationBLL().GetStationByStationID(stationID);
            if (station != null)
            {
                txtStationID.Text = station.StationID;
                txtStationName.Text = station.StationName;
                txtPlace.Text = station.Address;
                txtDescription.Text = station.Description;
                txtStationID.ReadOnly = true;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int result = 0;
            if (!string.IsNullOrEmpty(hdfStationID.Value))
            {
                result = new StationBLL().UpdateStation(hdfStationID.Value, txtStationName.Text, txtPlace.Text, txtDescription.Text);
            }
            else
            {
                result = new StationBLL().InsertStation(txtStationID.Text, txtStationName.Text, txtPlace.Text, txtDescription.Text);
            }

            if (result > 0) Response.Redirect("CMS_StationList.aspx");
        }
    }
}